<template>
  <div class="chart-detail">
    <div class="" style="height:70px;" v-show="canShow"></div>
    <!-- 头部显示 -->
    <div class="detail-top">
      <div class="top-left">
        个人养老保险待遇月度分析
      </div>
      <div class="top-right" @click="goBack">
        返回
      </div>
    </div>
    <!-- 详情 -->
    <div class="detail-box" :style="{height:height+'px'}">
      <!-- 详情头部 -->
      <h3 class="chart-title">
        <div class="area-title-name" @mouseenter="mouseenter()"
             @mouseleave="mouseleave()">{{ areaTitle }} &nbsp; | &nbsp; {{title}}年
        </div>
      </h3>
      <div class="chart-title-floating" v-show="showFloating">
        <span>统筹区: {{ this.propParams.areaName }}</span><br>
        <span>年份: {{ title }}</span>
      </div>
      <div id="pension-expenditure-analysis-person-detail" class="detail-content"></div>
    </div>
  </div>
</template>
<script>

  import '../../assets/css/chartDetail.css'
  import {loadingOpt} from '../../assets/js/utils.js'
  import {getGeRenYueDuYangLaoAnalysis} from '../../api/api'


  export default {
    name: 'chartDetail',
    props: ['isChange'],
    data () {
      return {
        title: '',
        areaTitle: '',
        isShowChart: true,
        height: 800,
        canShow: true,
        showFloating: false,
        propParams: {}
      }
    },
    mounted () {

      this.propParams = this.$route.query;
      this.title = this.propParams.year;
      this.areaTitle = this.filterLogText(this.propParams.areaName);
      this.getContentH();
      setTimeout(() => {
        this.myChart = this.$echarts.init(document.getElementById('pension-expenditure-analysis-person-detail'));
        this.myChart.showLoading(loadingOpt);
        this.getData();
      }, 101)
    },
    watch: {
      isChange () {
        this.getContentH();
      },
    },
    methods: {
      getData () {
        getGeRenYueDuYangLaoAnalysis('liNianYangLao/GeRenYueDuYangLaoAnalysis', this.propParams.year, this.propParams.areaCode,)
          .then(response => {
            let json = response.data;
            if (response.code === 0) {
              this.myChart.hideLoading();
              this.drawLine(json)
            }
          })
      },

      // 设置高度
      getContentH () {
        setTimeout(() => {
          let height = document.documentElement.clientHeight;
          let barHeight = 0;
          if (document.querySelector('.el-header')) {
            this.canShow = true;
            barHeight = document.querySelector('.el-header').clientHeight;
          } else {
            this.canShow = false;
          }
          this.height = height - barHeight - 96;
        }, 100);
        if (this.myChart) {
          setTimeout(() => {
            this.myChart.resize();
          }, 301)
        }
      },
      goBack () {
        this.$router.push(this.propParams.hash.slice(1,this.propParams.hash.length));
        // this.$router.go(-1);
      },
      // 字数太长省略
      filterLogText (value, leng = 7) {
        if (value.length <= leng) {
          return value;
        } else {
          return value.slice(0, leng) + '...';
        }
      },
      drawLine (json) {
        let option = {
          grid: [//此参数可在一个dom中划分多个图表 可设置不同图表的位置
            {
              top: '10%',
              height: '12%',
              tooltip: {
                formatter: "{b}</br>{c}%" //给当前坐标系添加弹窗
              }
            },
            {
              top: '36%',
              height: '46%',
              tooltip: {
                show: false,
              }
            }
          ],
          legend: [
            {
              left: 'center',
              icon: 'circle',
              itemWidth: 10,
              itemHeight: 10,
              itemGap: 20,
              selectedMode: false,
              tooltip: {
                show: true,
                formatter: function (params) {
                  return '个人养老保险待遇平均值增长率';
                }
              },
              data: ['个人养老保险待遇平均值增长率'],
              textStyle: {
                color: '#fff'
              },
            },
            {
              top: '92%',
              left: 'center',
              icon: 'rect',
              itemWidth: 10,
              itemHeight: 10,
              itemGap: 20,
              selectedMode: false,
              data: ['个人养老保险待遇金额平均值'],
              textStyle: {
                color: '#fff'
              },
            }],
          tooltip: {
            show: true,
            textStyle: {
              fontSize: 12
            }
          },
          xAxis: [
            {
              data: json.yueList,
              show: true,
              axisLine: {
                show: true, // 显示x轴坐标线并设置为虚线
                lineStyle: {
                  color: '#294363'
                }
              },
              axisTick: {
                show: false // 不显示x轴坐标的刻度
              },
              axisLabel: {
                show: false // 不显示x轴坐标数值
              }
            },
            {
              data: json.yueList,
              gridIndex: 1,
              axisLine: { // x轴的横线是否显示
                show: true,
                lineStyle: {
                  color: '#2a4565'
                }
              },
              axisTick: { // x轴的刻度短竖线是否显示
                show: false
              },
              axisLabel: {
                formatter: '{value}',
                textStyle: {
                  color: '#fff'
                }
              },
            }
          ],
          yAxis: [
            {
              splitLine: {
                show: false,
              },
              position: 'right',
              splitNumber: 2,
              axisLine: {
                show: false,
              },
              axisTick: {
                show: false
              },
              axisLabel: {
                formatter: '{value}',
                textStyle: {
                  color: '#fff'
                }
              },
            },
            {
              name: '单位 : 元',
              nameTextStyle: {
                color: '#fff'
              },
              splitLine: {
                show: true,
                lineStyle: {
                  color: '#2a4565',
                  type: 'dashed',
                }
              },
              gridIndex: 1,
              axisLine: {
                show: true,
                lineStyle: {
                  color: '#2a4565',
                }
              },
              axisTick: {
                show: false
              },
              axisLabel: {
                formatter: '{value}',
                textStyle: {
                  color: '#fff'
                }
              },
            }
          ],
          series: [ // 折线
            {
              type: 'line',
              color: '#129cff',
              symbol: 'circle',
              showSymbol: true,
              name: '个人养老保险待遇平均值增长率',
              data: json.avgRatio,
              symbolSize: 10,
              tooltip: {
                show: true,
                formatter: function (params) {
                  return `<span style="color:#fff">个人养老保险待遇平均值增长率</span></br>
                          <span style="color:#fff">月份: ${params.name}</span></br>
                          <span style="color:#fff">增长率: ${params.value}%</span>`
                }
              },
              label: {
                show: true,
                position: 'top',
                formatter: '{c}%',
                color: '#fff'
              }
            },
            {
              type: 'bar',
              name: '个人养老保险待遇金额平均值',
              data: json.avgList,
              color: '#ff6a93',
              barWidth: '30%',
              xAxisIndex: 1,
              yAxisIndex: 1,
              z: 2,
              tooltip: {
                show: true,
                formatter: function (params) {
                  return `<span style="color:#fff">个人养老保险待遇金额平均值</span></br>
                          <span style="color:#fff"">月份: ${params.name}</span></br>
                          <span style="color:#fff">平均值: ${params.value}元</span>`
                }
              },
              label: {
                show: true,
                color: '#fff',
                position: 'top',
                formatter: function (params) {
                  return params.value;
                }
              },
              markLine: {
                label: {
                  show: false,
                },
                symbol: 'none',//设置平均线的没样式
                data: [
                  {
                    type: 'average',
                    name: '年度平均值',
                    yAxis: json.yearavg,
                    tooltip: {
                      show: true,
                      formatter (params) {
                        return `<span style="color:#fff"">年度平均值</span></br>
                                <span style="color:#fff">${params.value}元</span>`
                      }
                    }
                  },
                ],
                lineStyle: {
                  color: '#129cff',
                  type: 'solid'
                }
              }
            },
          ],
        };

        this.myChart.setOption(option);
      },
      mouseenter () {
        this.showFloating = true;
      },
      mouseleave () {
        this.showFloating = false;
      },
    }
  }
</script>
<style scoped>
</style>
